<script>
import warehouseApi from "@/api/warehouse/warehouse";

export default {
  data() {
    return {
      formData: {
      },
      tableData: null,
      current: 1, // 当前页
      limit: 1, // 每页显示记录数
      total: 0, // 总记录数
      warehouseInfo: {
        name: '',
        address: '',
        account: ''
      },
      newWarehouse:{
        name:'',
        address:'',
        account:''
      },
      dialogVisible:false,
      rules: {
        name: [
          { required: true, message: '请输入仓库名称', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入产品品牌', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入负责人姓名', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getdata()
  },
  methods: {
    // 获取数据
    getdata() {
      this.formData.page = this.current
      this.formData.limit = this.limit
      warehouseApi.getAllWarehouse(this.formData).then(
        res => {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      )
    },
    getPagedata(page = 1) {
      this.current = page
      this.formData.page = this.current
      this.formData.limit = this.limit
      warehouseApi.getAllWarehouse(this.formData).then(
        res => {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      )
    },
    onStatusChange(warehouse){
      //发送请求，修改仓库状态
      console.log(warehouse);
      warehouseApi.updateWarehouseStatus(warehouse).then(
        res => {
          this.$message({
            type:'success',
            message:'修改仓库状态成功'
          })
        }
      )
    },
    addWarehouse(){
      //添加仓库
      console.log(this.newWarehouse);
      warehouseApi.addNewWarehouse(this.newWarehouse).then(
        res => {
          if (res.code === 500) {
            this.$message({
              type: 'warning',
              message: '添加仓库失败!'
            })
          }
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '添加仓库成功'
            })
            this.dialogVisible = false
            this.newWarehouse = {}
            // 刷新页面
            this.getdata()
          }
        }
      )
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.$message('取消操作')
          done()
        })
        .catch(_ => {})
    }
  }
}
</script>

<template>
  <div class="warehouse">
    <div class="search">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="仓库名称">
          <el-input v-model="formData.name" placeholder="仓库名称" clearable/>
        </el-form-item>
        <el-form-item label="仓库地址">
          <el-input v-model="formData.address" placeholder="仓库地址" clearable/>
        </el-form-item>
        <el-form-item label="负责人姓名">
          <el-input v-model="formData.account" placeholder="负责人姓名" clearable/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getdata" style="border-radius: 10px">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="dialogVisible = true" style="border-radius: 10px">添加</el-button>
        </el-form-item>
      </el-form>

    </div>

    <div>
      <el-table :data="tableData" border>
        <el-table-column prop="id" label="仓库ID" />
        <el-table-column prop="name" label="仓库名称" />
        <el-table-column prop="address" label="仓库地址" />
        <el-table-column prop="account.username" label="负责人姓名" />

        <el-table-column label="状态" prop="status">
          <template #default="{row}">
            <el-switch
              v-model="row.status"
              inline-prompt
              style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
              :active-value="0"
              :inactive-value="1"
              active-text="正常"
              inactive-text="禁用"
              @change="onStatusChange(row)"
            />
          </template>
        </el-table-column>

        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="updateTime" label="仓库更新时间" />

      </el-table>

      <el-dialog
        title="添加新仓库"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >

        <el-form :model="newWarehouse" label-suffix=":">
          <el-form-item label="仓库名称">
            <el-input v-model="newWarehouse.name" autocomplete="off" clearable/>
          </el-form-item>
          <el-form-item label="仓库地址">
            <el-input v-model="newWarehouse.address" autocomplete="off" clearable/>
          </el-form-item>

          <el-form-item label="仓库负责人" prop="model">
            <el-select v-model="newWarehouse.account" placeholder="请选择仓库负责人" style="width: 300px">
              <el-option
                v-for="item in modelclass"
                :key="item.id"
                :label="item.info"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" style="border-radius: 10px">取 消</el-button>
        <el-button type="primary" @click="addWarehouse" style="border-radius: 10px">添加</el-button>
      </span>
      </el-dialog>
    </div>


    <div class="block">
      <el-pagination
        :page-size="limit"
        :total="total"
        style="padding:30px 0;text-align:center;"
        layout="total,prev,pager,next,jumper"
        @current-change="getPagedata"
      />
    </div>

  </div>
</template>



<style scoped lang="scss">
.warehouse{
  margin: 20px;
}
</style>



